clear all
set more off
pause on
set trace off
cap log close
set matsize 5000
set maxvar 32767


	loc controls_rdd  winter spring summer fall
	
	*Seasonal Trend
		loc seasonXrunning
		loc x
			
		foreach control of loc controls_rdd {
			loc x `control'##c.running_var `control'##c.running_var2
			loc seasonXrunning `seasonXrunning' `x' 
		}



		use "/Users/mateo/Dropbox/Land_Data_Analysis/WP_Replication/Datasets/RDD_all_attacks.dta", clear
				
			preserve
			
				use `"/Users/mateo/Dropbox/Land_Data_Analysis/WP_Replication/Datasets/master_violence_landtitle_caracteristicas"', clear
 								
				keep if area_titul_comunidades_negras_cs!=0
				duplicates drop codmpio, force
				keep codmpio
				
				tempfile afro_comm
				save `afro_comm', replace
			
			restore
			
			preserve
			
				use `"/Users/mateo/Dropbox/Land_Data_Analysis/WP_Replication/Datasets/master_violence_landtitle_caracteristicas"', clear
 								
				keep if area_resguardos_indigenas_cs!=0
				duplicates drop codmpio, force
				keep codmpio
				
				tempfile indg_comm
				save `indg_comm', replace
			
			restore
			
			
			preserve
				use "/Users/mateo/Dropbox/Land_Data_Analysis/WP_Replication/Datasets/master_violence_landtitle_caracteristicas", clear
				
				collapse (mean) total_plots_rfmd_1960_85_1000, by(coddepto codmpio)
				
				gen plot_reformed_dumm=(total_plots_rfmd_1960_85_1000>0)
				replace plot_reformed_dumm=. if total_plots_rfmd_1960_85_1000==.
				
				keep if plot_reformed_dumm==1
				keep codmpio
				
				tempfile plot_reformed_dumm
				save `plot_reformed_dumm', replace
				
			restore
			
			preserve
				use "/Users/mateo/Dropbox/Land_Data_Analysis/WP_Replication/Datasets/master_violence_landtitle_caracteristicas", clear
				
				collapse (mean) any_title_placebo, by(coddepto codmpio)
				
				keep if any_title_placebo==1
				keep codmpio
				
				tempfile any_title_placebo
				save `any_title_placebo', replace
				
			restore
			
			
			merge m:1 codmpio using `afro_comm'
			
			drop if _m==2
			gen afro_comm=.
			replace afro_comm=1 if _m==3
			replace afro_comm=0 if _m==1
			drop _m			
			
			merge m:1 codmpio using `indg_comm'
			
			drop if _m==2
			gen indg_comm=.
			replace indg_comm=1 if _m==3
			replace indg_comm=0 if _m==1
			drop _m
			
			merge m:1 codmpio using `plot_reformed_dumm'
			
			drop if _m==2
			gen plot_reformed_dumm=.
			replace plot_reformed_dumm=1 if _m==3
			replace plot_reformed_dumm=0 if _m==1
			drop _m
			
			merge m:1 codmpio using `any_title_placebo'
			
			drop if _m==2
			gen any_title_placebo=.
			replace any_title_placebo=1 if _m==3
			replace any_title_placebo=0 if _m==1
			drop _m
		
			keep if !mi(threshold_wk)

			
			
			
			
			
			
			** Regressions
			
			xi: areg as_attacks title running_var running_var2 i.Año if running_var>-10 & running_var<10 & afro_comm==1,  cluster( dow ) absorb( codmpio)	
		
		** Graphs 
		
			* binscatter
			preserve
				replace running_var=. if running_var==0
				binscatter as_attacks running_var if running_var>-10 & running_var<10 & afro_comm==1, rd(0) absorb(codmpio) line(qfit)
			restore
			
			xi: areg as_attacks `seasonXrunning', cluster(codmpio) absorb(Año)
			predict resid, resid
			
		** Titled & Placebo Municipalities
	
			foreach x in 2 {
			
				preserve
					
					replace running_var=. if running_var==0
	 
					tempfile analysis
					save `analysis', replace
					
					** Title (Main Sample)
									
					lpoly resid running_var if (running_var>0 & running_var<10) & afro_comm==1, kernel(triangle) bwidth(10) degree(`x')  generate(x s) se(se) nograph
					keep x s se 
					drop if x==.
					tempfile RD
					save `RD', replace
					
					
					use `analysis', clear
					lpoly resid running_var  if (running_var>-10 & running_var<0) & afro_comm==1, kernel(triangle) bwidth(10) degree(`x')  generate(x s) se(se) nograph
					keep x s se 
					drop if x==.
					append using `RD'
					
					g ciplus=s+1.64*se
					g ciminus=s-1.64*se
					keep if abs(x)<10
					save `RD', replace
					
					
					*---generate bins for taking averages---*
					
					use `analysis', clear
					keep if abs(running_var)<10 & afro_comm==1
					
					gen bin1=.
					foreach X of num -10(1)10 {
						di "`X'"
						replace bin=-`X' if (running_var>=-`X' & running_var<(-`X'+1) & running_var<0)
						replace bin=`X' if (running_var>`X' & running_var<=(`X'+1))
					}
					tab bin1
					
					drop if bin1==.
					collapse resid running_var, by(bin1)
									
					append using `RD'
					
					tempfile title
					save `title', replace
					
					** No Title (Placebo)
					
					use `analysis', clear

					lpoly resid running_var if (running_var>0 & running_var<10) & indg_comm==1, kernel(triangle) bwidth(10) degree(`x')  generate(x s) se(se) nograph
					keep x s se
					ren s s_no
					ren x x_no
					ren se se_no
					drop if x==.
					tempfile RDno
					save `RDno', replace
					
					
					use `analysis', clear
					lpoly resid running_var  if (running_var>-10 & running_var<0) & indg_comm==1, kernel(triangle) bwidth(10) degree(`x')  generate(x s) se(se) nograph
					keep x s se
					ren s s_no
					ren x x_no
					ren se se_no
					drop if x==.
					append using `RDno'
					
					g ciplus_no=s_no+1.64*se_no
					g ciminus_no=s_no-1.64*se_no
					keep if abs(x)<10
					save `RDno', replace
					
					
					*---generate bins for taking averages---*
					
					use `analysis', clear
					keep if abs(running_var)<10 & indg_comm==1
					
					gen bin1=.
					foreach X of num -10(1)10 {
						di "`X'"
						replace bin=-`X' if (running_var>=-`X' & running_var<(-`X'+1) & running_var<0)
						replace bin=`X' if (running_var>`X' & running_var<=(`X'+1))
					}
					tab bin1
					
					drop if bin1==.
					collapse resid running_var, by(bin1)
					
					ren resid resid_no
					ren running_var running_var_no
									
					append using `RDno'
					
					save `RDno', replace
					
					** Plots Reformed 1988 -2001
					
					use `analysis', clear

					lpoly resid running_var if (running_var>0 & running_var<10) & any_title_placebo==1, kernel(triangle) bwidth(10) degree(`x')  generate(x s) se(se) nograph
					keep x s se
					ren s s_no_placebo
					ren x x_no_placebo
					ren se se_no_placebo
					drop if x==.
					tempfile RDno_placebo
					save `RDno_placebo', replace
					
					
					use `analysis', clear
					lpoly resid running_var  if (running_var>-10 & running_var<0) & any_title_placebo==1, kernel(triangle) bwidth(10) degree(`x')  generate(x s) se(se) nograph
					keep x s se
					ren s s_no_placebo
					ren x x_no_placebo
					ren se se_no_placebo
					drop if x==.
					append using `RDno_placebo'
					
					g ciplus_no_placebo=s_no_placebo+1.64*se_no_placebo
					g ciminus_no_placebo=s_no_placebo-1.64*se_no_placebo
					keep if abs(x)<10
					save `RDno_placebo', replace
					
					
					*---generate bins for taking averages---*
					
					use `analysis', clear
					keep if abs(running_var)<10 & any_title_placebo==1
					
					gen bin1=.
					foreach X of num -10(1)10 {
						di "`X'"
						replace bin=-`X' if (running_var>=-`X' & running_var<(-`X'+1) & running_var<0)
						replace bin=`X' if (running_var>`X' & running_var<=(`X'+1))
					}
					tab bin1
					
					drop if bin1==.
					collapse resid running_var, by(bin1)
					
					ren resid resid_no_placebo
					ren running_var running_var_no_placebo
									
					append using `RDno_placebo'
					
					save `RDno_placebo', replace
					
					** Plots reformed (1960 - 1985)
					
					use `analysis', clear

					lpoly resid running_var if (running_var>0 & running_var<10) & plot_reformed_dumm==1, kernel(triangle) bwidth(10) degree(`x')  generate(x s) se(se) nograph
					keep x s se
					ren s s_no1
					ren x x_no1
					ren se se_no1
					drop if x==.
					tempfile RDno1
					save `RDno1', replace
					
					
					use `analysis', clear
					lpoly resid running_var  if (running_var>-10 & running_var<0) & plot_reformed_dumm==1, kernel(triangle) bwidth(10) degree(`x')  generate(x s) se(se) nograph
					keep x s se
					ren s s_no1
					ren x x_no1
					ren se se_no1
					drop if x==.
					append using `RDno1'
					
					g ciplus_no=s_no1+1.64*se_no1
					g ciminus_no=s_no1-1.64*se_no1
					keep if abs(x)<10
					save `RDno1', replace
					
					
					*---generate bins for taking averages---*
					
					use `analysis', clear
					keep if abs(running_var)<10 & plot_reformed_dumm==1
					
					gen bin1=.
					foreach X of num -10(1)10 {
						di "`X'"
						replace bin=-`X' if (running_var>=-`X' & running_var<(-`X'+1) & running_var<0)
						replace bin=`X' if (running_var>`X' & running_var<=(`X'+1))
					}
					tab bin1
					
					drop if bin1==.
					collapse resid running_var, by(bin1)
					
					ren resid resid_no1
					ren running_var running_var_no1
									
					append using `RDno1'
					
					append using `RDno'
					
					append using `RDno_placebo'

					append using `title'
					
					
					
					***No Confidence Bands*** 
					
					twoway (connected s x if x>0, sort msymbol(none) clcolor(black) clpat(solid) clwidth(medthick))  (connected s_no x_no if x>0, sort msymbol(none) clcolor(red) clpat(dash) clwidth(med)) (connected s_no1 x_no1 if x>0, sort msymbol(none) clcolor(blue) clpat(dash) clwidth(med)) (connected s_no_placebo x_no_placebo if x>0, sort msymbol(none) clcolor(green) clpat(dash) clwidth(med)) /*
					(connected ciplus x if x>0, sort msymbol(none) clcolor(black) clpat(shortdash) clwidth(thin)) /*
					*/(connected ciminus x if x>0, sort msymbol(none) clcolor(black) clpat(shortdash) clwidth(thin)) 
					*/(connected s x if x<0, sort msymbol(none) clcolor(black) clpat(solid) clwidth(medthick)) (connected s_no x_no if x<0, sort msymbol(none) clcolor(red) clpat(dash) clwidth(med)) (connected s_no1 x_no1 if x<0, sort msymbol(none) clcolor(blue) clpat(dash) clwidth(med))  (connected s_no_placebo x_no_placebo if x<0, sort msymbol(none) clcolor(blue) clpat(dash) clwidth(med))/*
					(connected ciplus x if x<0, sort msymbol(none) clcolor(black) clpat(shortdash) clwidth(thin)) /*
					*/(connected ciminus x if x<0, sort msymbol(none) clcolor(black) clpat(shortdash) clwidth(thin)) 
					*/ (scatter resid running_var, sort msize(small) mcolor(black)) (scatter resid_no running_var_no,  sort msize(vsmall) mcolor(red) msymbol(triangle)) (scatter resid_no1 running_var_no1,  sort msize(vsmall) mcolor(blue) msymbol(square)) (scatter resid_no_placebo running_var_no_placebo,  sort msize(vsmall) mcolor(green) msymbol(diamond)), /*
					*/legend(order(5 6 4 7) label (5 "Afro-descendant Communities") label (6 "Indigenous Reservations") label (7 "Reformed Municipalities ('60 - '85)") label (4 "Reformed Municipalities ('88 - '00)")  row(2) size(small)  ) /*
					*/ /*legend(off)*/ graphregion(color(white)) /*
					*/ ytitle("All Attacks") xlabel(-10(2)10) xsc(r(-10 10)) ylabel(0(0.01)0.02) ysc(r(0 .02)) xtitle(Normalized Week) ylab(,nogrid) /*
					*/xline(0, lpattern(line) lwidth(thin) lc(black))/*
					*/ylabel(,angle(360)) subtitle("                	  {&larr}weeks {bf:before} title               weeks {bf:after} title {&rarr}                            ")					  
			
				
					graph export `"/Users/mateo/Desktop/RDiT_alternative_samples.png"', replace
					
				restore
							
			}
